package com.cwz.core.captcha;

import com.cwz.core.captcha.common.Fonts;
import com.cwz.core.captcha.core.AnimCaptcha;
import com.cwz.core.captcha.core.Captcha;
import com.cwz.core.captcha.support.CaptchaStyle;
import com.cwz.core.captcha.support.CaptchaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: w-demo
 * @description: 验证码调用封装类
 * @author: Wen
 **/
public class WCaptcha {
	public static final String SESSION_KEY = "happy-captcha";
	private CaptchaType type;
	private CaptchaStyle style;
	private Font font;
	private int width;
	private int height;
	private int length;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public static WCaptcha.Builder require(HttpServletRequest request, HttpServletResponse response) {
		return new WCaptcha.Builder(request, response);
	}

	/**
	 * @Description: 新增的请求方法接口
	 * @Author: Wen
	 * @return:
	 **/
	public static WCaptcha.Builder require() {
		return new WCaptcha.Builder();
	}

	public boolean finish() {
		try {
			boolean flag = false;
			if (this.style.equals(CaptchaStyle.IMG)) {
				Captcha captcha = new Captcha();
				captcha.setType(this.type);
				captcha.setWidth(this.width);
				captcha.setHeight(this.height);
				captcha.setLength(this.length);
				captcha.setFont(this.font);
				this.setHeader(this.response);
				this.request.getSession().setAttribute("happy-captcha", captcha.getCaptchaCode());
				captcha.render(this.response.getOutputStream());
				return true;
			} else if (this.style.equals(CaptchaStyle.ANIM)) {
				AnimCaptcha captcha = new AnimCaptcha();
				captcha.setType(this.type);
				captcha.setWidth(this.width);
				captcha.setHeight(this.height);
				captcha.setLength(this.length);
				captcha.setFont(this.font);
				this.setHeader(this.response);
				this.request.getSession().setAttribute("happy-captcha", captcha.getCaptchaCode());
				captcha.render(this.response.getOutputStream());
				return true;
			} else {
				return false;
			}
		} catch (Exception var3) {
			var3.printStackTrace();
			return false;
		}
	}

	/**
	 * @Description: 新增的返回方法（返回验证码和验证图片）
	 * @Author: Wen
	 * @return: java.util.Map<java.lang.String,java.lang.Object>
	 **/
	public Map<String, String> finishMap() {
		try {
			Map<String, String> map = new HashMap<>();
			if (this.style.equals(CaptchaStyle.IMG)) {
				Captcha captcha = new Captcha();
				captcha.setType(this.type);
				captcha.setWidth(this.width);
				captcha.setHeight(this.height);
				captcha.setLength(this.length);
				captcha.setFont(this.font);
				map.put("code", captcha.getCaptchaCode());
				map.put("imgBase64", captcha.toBase64());
				return map;
			} else if (this.style.equals(CaptchaStyle.ANIM)) {
				AnimCaptcha captcha = new AnimCaptcha();
				captcha.setType(this.type);
				captcha.setWidth(this.width);
				captcha.setHeight(this.height);
				captcha.setLength(this.length);
				captcha.setFont(this.font);
				map.put("code", captcha.getCaptchaCode());
				map.put("imgBase64", captcha.toBase64());
				return map;
			} else {
				return null;
			}
		} catch (Exception var3) {
			var3.printStackTrace();
			return null;
		}
	}

	public static boolean verification(HttpServletRequest request, String code, boolean ignoreCase) {
		if (code != null && !code.trim().equals("")) {
			String captcha = (String)request.getSession().getAttribute("happy-captcha");
			return ignoreCase ? code.equalsIgnoreCase(captcha) : code.equals(captcha);
		} else {
			return false;
		}
	}

	public static void remove(HttpServletRequest request) {
		request.getSession().removeAttribute("happy-captcha");
	}

	public void setHeader(HttpServletResponse response) {
		response.setContentType("image/gif");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0L);
	}

	private WCaptcha(WCaptcha.Builder builder) {
		this.type = builder.type;
		this.style = builder.style;
		this.font = builder.font;
		this.width = builder.width;
		this.height = builder.height;
		this.length = builder.length;
		this.request = builder.request;
		this.response = builder.response;
	}

	public static class Builder {
		private CaptchaType type;
		private CaptchaStyle style;
		private Font font;
		private int width;
		private int height;
		private int length;
		private final HttpServletRequest request;
		private final HttpServletResponse response;

		public Builder(HttpServletRequest request, HttpServletResponse response) {
			this.type = CaptchaType.DEFAULT;
			this.style = CaptchaStyle.IMG;
			this.font = Fonts.getInstance().defaultFont();
			this.width = 160;
			this.height = 50;
			this.length = 5;
			this.request = request;
			this.response = response;
		}

		/**
		 * @Description: 新增的构造方法
		 * @Author: Wen
		 * @return:
		 **/
		public Builder() {
			this.type = CaptchaType.DEFAULT;
			this.style = CaptchaStyle.IMG;
			this.font = Fonts.getInstance().defaultFont();
			this.width = 160;
			this.height = 50;
			this.length = 5;
			this.request = null;
			this.response = null;
		}

		public WCaptcha build() {
			return new WCaptcha(this);
		}

		public WCaptcha.Builder type(CaptchaType type) {
			this.type = type;
			return this;
		}

		public WCaptcha.Builder style(CaptchaStyle style) {
			this.style = style;
			return this;
		}

		public WCaptcha.Builder width(int width) {
			this.width = width;
			return this;
		}

		public WCaptcha.Builder height(int height) {
			this.height = height;
			return this;
		}

		public WCaptcha.Builder length(int length) {
			this.length = length;
			return this;
		}

		public WCaptcha.Builder font(Font font) {
			this.font = font;
			return this;
		}
	}
}
